#include "gotocontrol.h"
#include <QLineEdit>
#include <QListWidget>
#include <QVBoxLayout>
#include "projectmanager.h"
#include <QFileInfo>
#include <QKeyEvent>
#include <gotoedit.h>
#include "resources.h"

const int KRowHeight = 30;
const int KSpacing = 10;


GoToControl::GoToControl(QWidget *parent) :
    QDialog(parent)
{
    m_projectManager = NULL;
    m_gotoEdit = new GoToEdit;
    m_gotoEdit->setMinimumHeight(KRowHeight);
    m_dropDownMenu = new GoToList;
    m_dropDownMenu->setParentControl(this);
    m_dropDownMenu->setMinimumHeight(KRowHeight);
    m_dropDownMenu->setMouseTracking(true);

    QVBoxLayout * layout = new QVBoxLayout;
    setLayout(layout);

    layout->addWidget(m_gotoEdit);
    layout->addWidget(m_dropDownMenu);
    connect(m_gotoEdit, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged(QString)));
    connect(m_gotoEdit, SIGNAL(returnPressed()), this, SLOT(onReturnPressed()));
    connect(m_dropDownMenu, SIGNAL(itemEntered(QListWidgetItem*)), this, SLOT(onItemHovered(QListWidgetItem*)));
    connect(m_dropDownMenu, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(onItemChosen(QListWidgetItem*)));
    connect(m_dropDownMenu, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(onItemChosen(QListWidgetItem*)));
    QPixmap pixmapLua;
    pixmapLua.loadFromData(Resources::lua_png, sizeof(Resources::lua_png));
    m_luaImage.addPixmap(pixmapLua);
    QPixmap pixmapGlobal;
    pixmapGlobal.loadFromData(Resources::global_png, sizeof(Resources::global_png));
    m_globalImage.addPixmap(pixmapGlobal);
}





GoToControl::~GoToControl()
{
    delete m_gotoEdit;
    delete m_dropDownMenu;
}

void GoToControl::setProjectManager(ProjectManager *aManager)
{
    m_projectManager = aManager;
}

void GoToControl::onTextChanged(QString aText)
{
    updateList();
    updateLayout();
}


void GoToControl::updateList()
{
    m_dropDownMenu->clear();
    QString word = m_gotoEdit->text();
    if(word.length() == 0)
        return;
    //get all globals first
    QStringList globals;
    QStringList globalLinks;
    if(m_projectManager)
        m_projectManager->getAllMatchingGlobals(word, globals, globalLinks);
    //then files
    QStringList files;
    QStringList fileLinks;
    if(m_projectManager)
    {
        QStringList allFiles;
        m_projectManager->getAllFiles(allFiles);
        for(int i = 0; i < allFiles.count(); i++)
        {
            QString fileName = QFileInfo(allFiles[i]).fileName();
            if(fileName.startsWith(word))
            {
                files.append(fileName);
                fileLinks.append("global:0:" + allFiles[i] + ":temp" );
            }

        }
    }

    if(globals.count() == 0 && files.count() == 0)
        return;
    for(int i = 0; i < globals.count(); i++)
    {
        QListWidgetItem * item = new QListWidgetItem(m_globalImage, globals[i]);
        item->setData(Qt::UserRole, globalLinks[i]);
        m_dropDownMenu->addItem(item);
    }


    for(int i = 0; i < files.count(); i++)
    {
        QListWidgetItem * item = new QListWidgetItem(m_luaImage, files[i]);
        item->setData(Qt::UserRole, fileLinks[i]);
        m_dropDownMenu->addItem(item);
    }
    m_dropDownMenu->sortItems();
}

void GoToControl::setAvalaibleHeight(int aHeight)
{
    m_avalaibleHeight = aHeight;
}


void GoToControl::showEvent(QShowEvent *e)
{
    m_gotoEdit->setText("");
    updateLayout();
    QDialog::showEvent(e);
}

void GoToControl::updateLayout()
{
    if(m_dropDownMenu->count() == 0)
    {
        m_dropDownMenu->hide();
        this->setFixedHeight(m_gotoEdit->height() + KSpacing);
    }
    else
    {
        m_dropDownMenu->show();
        //m_dropDownMenu->setFixedHeight(m_dropDownMenu->count()*20);
        //m_dropDownMenu->setFixedWidth(this->width());
       //m_dropDownMenu->move(m_gotoEdit->x(), m_gotoEdit->y() + m_gotoEdit->height());
        int entireHeight = m_gotoEdit->height() + m_dropDownMenu->count()*KRowHeight + KSpacing;
        if(entireHeight > m_avalaibleHeight)
            entireHeight = m_avalaibleHeight;
        this->setFixedHeight(entireHeight);

    }
}


void GoToControl::keyPressEvent(QKeyEvent *e)
{
    int key = e->key();
    if(key == Qt::Key_Escape)
    {
        this->close();
        e->accept();
        return;
    }

    if(key == Qt::Key_Down || key == Qt::Key_Up)
    {
        if(m_dropDownMenu->count() > 0)
        {
            m_dropDownMenu->setFocus();
            m_dropDownMenu->item(0)->setSelected(true);
            m_dropDownMenu->setCurrentItem(m_dropDownMenu->item(0));
            e->accept();
            return;
        }
    }
    if(key == Qt::Key_Enter)
    {
        onReturnPressed();
        e->accept();
        return;
    }
    m_gotoEdit->setFocus();
    m_gotoEdit->keyPressEvent(e);
    QDialog::keyPressEvent(e);
}

void GoToControl::onItemHovered(QListWidgetItem * aItem)
{
    aItem->setSelected(true);
}

void GoToControl::onItemChosen(QListWidgetItem * aItem)
{
    QString link = aItem->data(Qt::UserRole).toString();
    emit itemChosen(link);
    close();
}

void GoToControl::onReturnPressed()
{
    if(m_dropDownMenu->count() > 0)
    {
        QListWidgetItem * item = m_dropDownMenu->currentItem();
        if(item)
        {
            onItemChosen(item);
        }
    }
}

